/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rockpaperscissor;

import rockpaperscissor.Server.GameFactory;

/**
 *
 * @author DahlstrÃ¶m
 */
public class Game {

    private int p1Choice, p2Choice;
    private GameFactory gf;
    private int winner;

    public Game() {
        p1Choice = -1;
        p2Choice = -1;
    }

    public synchronized void addChoice(int choice, int pId) {
        if (pId == 1) {
            p1Choice = choice;
        } else if (pId == 2) {
            p2Choice = choice;
        }

        if (p1Choice != -1 && p2Choice != -1) {
            determineWinner();
        }
    }

    public int getWinner() {
        p1Choice = -1;
        p2Choice = -1;
        return winner;
    }

    public void determineWinner() {
        if (p1Choice == 1) {
            if (p2Choice == 1) {
                winner = 0;
            } else if (p2Choice == 2) {
                winner = 2;
            } else if (p2Choice == 3) {
                winner = 1;
            }
        } else if (p1Choice
                == 2) {
            if (p2Choice == 1) {
                winner = 1;
            } else if (p2Choice == 2) {
                winner = 0;
            } else if (p2Choice == 3) {
                winner = 2;
            }
        } else if (p1Choice
                == 3) {
            if (p2Choice == 1) {
                winner = 2;
            } else if (p2Choice == 2) {
                winner = 1;
            } else if (p2Choice == 3) {
                winner = 0;
            }
        }
    }
}
